package io.renren.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import io.renren.entity.CustomSettlementEntity;
import io.renren.entity.SocialSecurityAreaBasicEntity;
import io.renren.service.CustomSettlementService;
import io.renren.service.SocialSecurityAreaBasicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.ServiceSettlementMainEntity;
import io.renren.service.ServiceSettlementMainService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;


/**
 * 劳务结算汇总表
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-19 16:36:21
 */
@RestController
@RequestMapping("servicesettlementmain")
public class ServiceSettlementMainController extends AbstractController{
	@Autowired
	private ServiceSettlementMainService serviceSettlementMainService;

	@Autowired
	private CustomSettlementService customSettlementService;

	@Autowired
	private SocialSecurityAreaBasicService socialSecurityAreaBasicService;


	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("servicesettlementmain:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<ServiceSettlementMainEntity> serviceSettlementMainList = serviceSettlementMainService.queryList(query);
		int total = serviceSettlementMainService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(serviceSettlementMainList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("servicesettlementmain:info")
	public R info(@PathVariable("id") Integer id){
		ServiceSettlementMainEntity serviceSettlementMain = serviceSettlementMainService.queryObject(id);
		//查出对应的结算点对象
		CustomSettlementEntity customSettlementEntity = customSettlementService.queryObject(serviceSettlementMain.getSettlementid());
		//查出对应的区域基数对象
		Map<String,Object> params = new HashMap();
		params.put("index",customSettlementEntity.getAreaBaseId());
		params.put("year",serviceSettlementMain.getYear());
		params.put("month",serviceSettlementMain.getMonth());
		List<SocialSecurityAreaBasicEntity> socialSecurityAreaBasicEntities = socialSecurityAreaBasicService.queryByIndex(params);
		SocialSecurityAreaBasicEntity socialSecurityAreaBasicEntity = socialSecurityAreaBasicEntities.get(0) ;
		//为劳务结算汇总对象设置基数

		//单位部分
		serviceSettlementMain.setDwyjGongsYs(socialSecurityAreaBasicEntity.getGongsbxBasic());
		serviceSettlementMain.setDwyjYanglYs(socialSecurityAreaBasicEntity.getYanglbxBasic());
		serviceSettlementMain.setDwyjShiyYs(socialSecurityAreaBasicEntity.getShiybxBasic());
		serviceSettlementMain.setDwyjYilYs(socialSecurityAreaBasicEntity.getYilbxBasic());
		serviceSettlementMain.setDwyjShengyYs(socialSecurityAreaBasicEntity.getShengybxBasic());
		serviceSettlementMain.setDwyjDabYs(socialSecurityAreaBasicEntity.getDabinBasic());
		serviceSettlementMain.setDwyjGongjjYs(socialSecurityAreaBasicEntity.getGongjjBasic());

		//个人部分
		serviceSettlementMain.setGryjYanglYs(socialSecurityAreaBasicEntity.getYanglbxBasic());
		serviceSettlementMain.setGryjShiyYs(socialSecurityAreaBasicEntity.getShiybxBasic());
		serviceSettlementMain.setGryjYilYs(socialSecurityAreaBasicEntity.getYilbxBasic());
		serviceSettlementMain.setGryjDabYs(socialSecurityAreaBasicEntity.getDabinBasic());
		serviceSettlementMain.setGryjGongjjYs(socialSecurityAreaBasicEntity.getGongjjBasic());

		return R.ok().put("serviceSettlementMain", serviceSettlementMain);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("servicesettlementmain:save")
	public R save(@RequestBody ServiceSettlementMainEntity serviceSettlementMain){
		serviceSettlementMainService.save(serviceSettlementMain);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("servicesettlementmain:update")
	public R update(@RequestBody ServiceSettlementMainEntity serviceSettlementMain){
		serviceSettlementMainService.update(serviceSettlementMain);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("servicesettlementmain:delete")
	public R delete(@RequestBody Integer[] ids){
		serviceSettlementMainService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 信息
	 */
	@RequestMapping("/getSettlementmainInfo")
	@RequiresPermissions("servicesettlementmain:info")
	public R info(@RequestBody JSONObject jsonObject){
		/*ServiceSettlementMainEntity serviceSettlementMain = serviceSettlementMainService.queryObject(id);
		//查出对应的结算点对象
		CustomSettlementEntity customSettlementEntity = customSettlementService.queryObject(serviceSettlementMain.getSettlementid());
		//查出对应的区域基数对象
		*/
		ServiceSettlementMainEntity serviceSettlementMain = new ServiceSettlementMainEntity();
		Map<String,Object> params = new HashMap();
		params.put("index",jsonObject.getString("index"));
		params.put("year",jsonObject.getString("year"));
		params.put("month",jsonObject.getString("month"));
		List<SocialSecurityAreaBasicEntity> socialSecurityAreaBasicEntities = socialSecurityAreaBasicService.queryByIndex(params);
		SocialSecurityAreaBasicEntity socialSecurityAreaBasicEntity = socialSecurityAreaBasicEntities.get(0) ;
		//为劳务结算汇总对象设置基数

		//单位部分
		serviceSettlementMain.setDwyjGongsYs(socialSecurityAreaBasicEntity.getGongsbxBasic());
		serviceSettlementMain.setDwyjYanglYs(socialSecurityAreaBasicEntity.getYanglbxBasic());
		serviceSettlementMain.setDwyjShiyYs(socialSecurityAreaBasicEntity.getShiybxBasic());
		serviceSettlementMain.setDwyjYilYs(socialSecurityAreaBasicEntity.getYilbxBasic());
		serviceSettlementMain.setDwyjShengyYs(socialSecurityAreaBasicEntity.getShengybxBasic());
		serviceSettlementMain.setDwyjDabYs(socialSecurityAreaBasicEntity.getDabinBasic());
		serviceSettlementMain.setDwyjGongjjYs(socialSecurityAreaBasicEntity.getGongjjBasic());

		//个人部分
		serviceSettlementMain.setGryjYanglYs(socialSecurityAreaBasicEntity.getYanglbxBasic());
		serviceSettlementMain.setGryjShiyYs(socialSecurityAreaBasicEntity.getShiybxBasic());
		serviceSettlementMain.setGryjYilYs(socialSecurityAreaBasicEntity.getYilbxBasic());
		serviceSettlementMain.setGryjDabYs(socialSecurityAreaBasicEntity.getDabinBasic());
		serviceSettlementMain.setGryjGongjjYs(socialSecurityAreaBasicEntity.getGongjjBasic());

		return R.ok().put("serviceSettlementMain", serviceSettlementMain);
	}

	/**
	 * 审核
	 */
	@RequestMapping("/updateTrailStatus")
	@RequiresPermissions("servicesettlementmain:update")
	public R updateTrailStatus(@RequestBody JSONObject jsonObject){
		//serviceSettlementMainService.update(serviceSettlementMain);
		jsonObject.put("user",getUser());
		serviceSettlementMainService.updateTrailStatus(jsonObject);
		return R.ok();
	}
	
}
